/*
 * @Description:
 * @Author: TM_FULLNAME
 * @version:
 * @Date: 2024-11-04 14:13:34
 * @LastEditors: TM_FULLNAME
 * @LastEditTime: 2024-11-04 14:13:34
 */

import axios from "axios";

let key = "ccd32f216dcc4f25928714cd103c5f96";

const api = axios.create({
    baseURL: "https://restapi.amap.com/v3",
    timeout: 5000,
    //参数
    params: {
        key: key,
    },
});

//高德天气api，
//1:ip获取城市编码 武汉市 -> 110101
//2:天气信息 110101 -> weatherInfo

//获取逆地理编码
const FetchAdcode = (cityName) => {
    return api({
        url: "/ip?",
        method: "GET",
        params: {
            address: cityName,
        },
    });
};

//获取天气信息
const FetchWeather = (adcode) => {
    return api({
        url: "/weather/weatherInfo",
        method: "GET",
        params: {
            city: adcode,
        },
    });
};

const FetchWeatherInfoByIp = async () => {
    try {
        const response = await FetchAdcode();
        const { adcode } = response.data;
        const weatherInfo = await FetchWeather(adcode);
        return weatherInfo.data;
    } catch (error) {
        return error;
    }
};

export { FetchWeatherInfoByIp };
